﻿using Marbles.CMS.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Marbles.CMS.ContentTypes;

namespace Marbles.CMS.Administrator
{
    public partial class Admin : System.Web.UI.MasterPage
    {

        public string Title
        {
            get { return lblAction.Text; }
            set { lblAction.Text = value; }
        }

        public event MenuEventHandler MenuItemClicked;

        protected void Page_Load(object sender, EventArgs e)
        {
            lblAction.Text = this.Page.Title;
            if (!Page.IsPostBack)
            {
                LoadDynamicMenu();
            }
        }

        private void LoadDynamicMenu()
        {
            ContentManagerBase cntlr = new ContentManagerBase();
            Dictionary<string, string> items = cntlr.GetPageTypeEditors();


            foreach (string s in items.Keys)
            {
                MenuItem child = new MenuItem();
                child.Text = "New " + items[s];
                child.Value = "Edit." + s;
                AdminNavMenu.Items[1].ChildItems.Add(child);
            }

            MenuItem userchild = new MenuItem();
            userchild.Text = "New User";
            userchild.ToolTip = userchild.Text;
            userchild.Value = "NewUser";
            AdminNavMenu.Items[1].ChildItems.Add(userchild);
        }

        protected void AdminNavMenu_MenuItemClick(object sender, MenuEventArgs e)
        {
            switch (e.Item.Value)
            {
                case "Logout":
                    SiteController.Logout();
                    Response.Redirect("/Default.aspx", true);
                    break;
                case "Home":
                    Response.Redirect("/Default.aspx", true);
                    break;
                case "About":
                    dlgAbout.Show();
                    break;
                default:
                    if (MenuItemClicked != null)
                    {
                        MenuItemClicked(sender, e);
                    }
                    break;
            }
        }
    }
}
